/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useState } from 'react';
import { Form, Input, Button, message } from 'antd';
import type { WaybillFromNoIndexType } from '../types/WaybillFromNo';
import { getWaybillFromNo } from '@/services/customerSearch';
import { isObject } from 'lodash';
import CommonResetButton from './_common/CommonResetButton';
const FormItem = Form.Item;

interface FormType {
  waybill: string;
}

const WaybillFromNo = (props: WaybillFromNoIndexType) => {
  const { screenSize } = props;
  const [content, setContent] = useState<string>('');
  const [form] = Form.useForm();

  const onFinish = async (values: FormType) => {
    const response = await getWaybillFromNo(values);
    const { code, data, msg } = response;
    const obj = data; // 不这么写，报没有 bind_courier_no 错误，没找到在哪定义。
    if (code == 0) {
      if (isObject(data)) {
        const { bind_courier_no } = obj;
        setContent(bind_courier_no ? `工号为： ${bind_courier_no}` : '没有获取到数据');
      } else {
        setContent(data);
      }
    } else {
      setContent(msg);
      message.error(msg);
    }
  };

  const onReset = () => {
    form.resetFields();
    setContent('');
  };

  return (
    <>
      <Form
        form={form}
        onFinish={onFinish}
        layout={screenSize ? 'vertical' : 'inline'}
        style={{ marginBottom: 20 }}
      >
        <FormItem label={'单号'} name='waybill' rules={[{ required: true, message: '请输入单号' }]}>
          <Input allowClear style={{ width: 200 }} placeholder={'请输入单号'} />
        </FormItem>
        <FormItem>
          <Button type='primary' htmlType='submit' loading={false}>
            查询
          </Button>
          <CommonResetButton onReset={onReset} />
        </FormItem>
      </Form>
      <div>{content}</div>
    </>
  );
};

export default WaybillFromNo;
